package com.exam.userms;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.exam.userms.adapter.UserAdapter;
import com.exam.userms.dbmanage.helper.UserDataManager;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 主activity：
 * 1. 初始化数据
 * 2. 展示用户列表
 * 3. 操作用户动作
 * Created by guopei on 2020/6/28
 */
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    private float nowVersionCode,saveVersionCode;
    private static final String ISFIRSTLAUCH = "isFirstLauch";
    private static final String SAVEVERSIONCODE = "saveVersionCode";
    private static final String SAVESTATUS = "saveStatus";

    private Button addButton;
    private Button deleteButton;
    private Button sendButton;
    private Button clearButton;

    private UserDataManager mUserDataManager;         //用户数据管理类

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);

        if (mUserDataManager == null) {
            mUserDataManager = new UserDataManager(this);
            mUserDataManager.openDataBase();                              //建立本地数据库
        }

        // 项目初始化判断
        nowVersionCode = getVersionCode();
        SharedPreferences sp  = getSharedPreferences(ISFIRSTLAUCH,MODE_PRIVATE);
        saveVersionCode = sp.getFloat(SAVEVERSIONCODE,0);

        if (nowVersionCode > saveVersionCode) {
            //第一次安装启动
            SharedPreferences.Editor editor = sp.edit();
            editor.putFloat(SAVEVERSIONCODE, nowVersionCode);
            editor.putBoolean(SAVESTATUS, true);
            editor.commit();
            //初始化10条数据
            initTenUsers();
        }
        // 展示数据
        showUsers();
    }

    /**
     * 获取应用版本号
     * @return
     * @throws PackageManager.NameNotFoundException
     */
    private float getVersionCode(){
        float versionCode = 0;
        try {
            versionCode = getPackageManager().getPackageInfo(getPackageName(),0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionCode;
    }

    private void initTenUsers() {

        List<UserData> initList = new ArrayList<>();
        initList.add(new UserData("郭沛", "18156851950"));
        initList.add(new UserData("张三", "15502178009"));
        initList.add(new UserData("李四", "13567777770"));
        initList.add(new UserData("王五", "15502172039"));
        initList.add(new UserData("刘梅", "15502124038"));
        initList.add(new UserData("张涛", "15342172040"));
        initList.add(new UserData("刘德华", "15602172009"));
        initList.add(new UserData("张学友", "1872172076"));
        initList.add(new UserData("周杰伦", "19602172089"));
        initList.add(new UserData("爱德华", "15598172056"));
        initList.add(new UserData("李强", "13998172023"));
        initList.add(new UserData("李华", "18998172098"));

        mUserDataManager.batchInsertUser(initList);
    }

    public void showUsers() {

        ListView listView = (ListView) findViewById(R.id.list_user);

        //动态加载顶部View 和底部 View
        final LayoutInflater inflater = LayoutInflater.from(this);
        View headView = inflater.inflate(R.layout.listview_header, null, false);
        View footView = inflater.inflate(R.layout.listview_footer, null, false);

        //添加表头和表尾需要写在 setAdapter() 方法调用之前
        listView.addHeaderView(headView);
        listView.addFooterView(footView);


        List<UserData> users = mUserDataManager.findAll();

        //创建一个 userAdapter
        UserAdapter userAdapter = new UserAdapter((LinkedList<UserData>) users, getApplicationContext());

        listView = (ListView) findViewById(R.id.list_user);

        listView.setAdapter(userAdapter);
        listView.setOnItemClickListener(this);


        deleteButton =  (Button) findViewById(R.id.delete);
        sendButton = (Button) findViewById(R.id.allSend);
        clearButton = (Button) findViewById(R.id.clear);
        // 绑定删除事件
        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mUserDataManager.deleteUserData(userAdapter.deletingData());
            }
        });

        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userAdapter.send();
            }
        });

        clearButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userAdapter.clear();
            }
        });
}

    public void add_user(View view) {
        Intent intent3 = new Intent(MainActivity.this, Register.class);
        startActivity(intent3);
        finish();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //Toast.makeText(getApplicationContext(), "你点击了第" + position + "项", Toast.LENGTH_SHORT).show();
    }
}